package com.zy.dao.impl;

import com.zy.dao.UserDao;
import com.zy.entity.User;
import com.zy.utils.JdbcUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class UserDaoImpl implements UserDao {
    @Override
    public void addUser(User user) {
        String sql = "INSERT INTO tb_users (username, password, userType, balance) VALUES (?, ?, ?, ?)";
        try (Connection connection = JdbcUtil.getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
            preparedStatement.setString(1, user.getUserName());
            preparedStatement.setString(2, user.getPassword());
            preparedStatement.setInt(3, user.getUserType());
            preparedStatement.setDouble(4, user.getBalance());
            preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace(); // 打印异常信息
            throw new RuntimeException("添加用户失败", e); // 抛出运行时异常
        }
    }
} 